package com.mkl.ttxt.pd.controller;

import com.mkl.ttxt.common.bo.TtxtResult;
import com.mkl.ttxt.common.constant.BaseConstant;
import com.mkl.ttxt.pd.bo.Lineup;
import com.mkl.ttxt.pd.bo.Param;
import com.mkl.ttxt.pd.bo.Record;
import com.mkl.ttxt.pd.service.IBoxService;
import com.mkl.ttxt.pd.service.IPdService;
import com.mkl.ttxt.remote.util.TokenUtil;
import com.mkl.ttxt.remote.util.loginUtil.UserLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/box")
@Api(description = "box管理")
public class BoxController {

    @Autowired
    IPdService pdService;
    @Autowired
    IBoxService boxService;

    @UserLoginToken
    @RequestMapping(value = "/getAllMemberBox")
    @ResponseBody
    @ApiOperation(value = "获取所有成员Box", httpMethod = "POST")
    public TtxtResult getAllMemberBox(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(pdService.getMemberBox(userId));
        return ttxtResult;
    }

    @UserLoginToken
    @PostMapping("/excelImportBox")
    @ResponseBody
    @ApiOperation(value = "excel导入Box", httpMethod = "POST")
    public TtxtResult excelImportBox(@RequestParam("uploadfile") MultipartFile file,
                                        HttpServletRequest httpServletRequest){
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        try{
            int type = 0;
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            if ("xls".equals(ext)) {
                type = 1;
            }
            boxService.excelImportBox(file, userId, type);
        }catch (Exception e){
            ttxtResult.setCode(BaseConstant.FAILED);
            ttxtResult.setMessage(e.getMessage());
        }
        return ttxtResult;
    }

    @UserLoginToken
    @PostMapping("/excelImportLineup")
    @ResponseBody
    @ApiOperation(value = "excel导入阵容", httpMethod = "POST")
    public TtxtResult excelImportLineup(@RequestParam("uploadfile") MultipartFile file,
                                     HttpServletRequest httpServletRequest){
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        try{
            int type = 0;
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            if ("xls".equals(ext)) {
                type = 1;
            }
            boxService.excelImportLineup(file, userId, type);
        }catch (Exception e){
            ttxtResult.setCode(BaseConstant.FAILED);
            ttxtResult.setMessage(e.getMessage());
        }
        return ttxtResult;
    }

    @UserLoginToken
    @RequestMapping(value = "/getAllParam")
    @ResponseBody
    @ApiOperation(value = "获取所有参数", httpMethod = "POST")
    public TtxtResult getAllParam(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(boxService.getAllParam(userId));
        return ttxtResult;
    }

    @UserLoginToken
    @RequestMapping(value = "/initParam")
    @ResponseBody
    @ApiOperation(value = "初始化参数", httpMethod = "POST")
    public TtxtResult initParam(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        boxService.initParam(userId);
        return ttxtResult;
    }

    @UserLoginToken
    @RequestMapping(value = "/updateParam")
    @ResponseBody
    @ApiOperation(value = "修改参数", httpMethod = "POST")
    public TtxtResult updateParam(@RequestBody Param param, HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        boxService.updateParam(param, userId);
        return ttxtResult;
    }

    @UserLoginToken
    @RequestMapping(value = "/calParam")
    @ResponseBody
    @ApiOperation(value = "根据轴伤自动优化参数参数", httpMethod = "POST")
    public TtxtResult calParam(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        boxService.calParam(userId);
        return ttxtResult;
    }

    @UserLoginToken
    @RequestMapping(value = "/recordDamage")
    @ResponseBody
    @ApiOperation(value = "记刀", httpMethod = "POST")
    public TtxtResult recordDamage(@RequestBody Record record, HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        boxService.recordDamage(record, userId);
        return ttxtResult;
    }
}
